<template>
  <div class="login">
    <!-- 头部 -->
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <!-- 主体 -->
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" v-model="mobile" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input class="inp" v-model="picCode" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="picUrl" :src="picUrl" alt="" @click="getPicCode">
        </div>
        <div class="form-item">
          <input v-model="msgCode" @keyup.enter="login" maxlength="6" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getCode" :disabled="codeDisabled">{{ codeMsg }}</button>
        </div>
      </div>

      <div @click="login" class="login-btn">登录</div>
    </div>
  </div>
</template>

<script>
import { codeLogin, getPicCode, getSmsCode } from '@/api/login'

export default {
  name: 'LoginIndex',
  data () {
    return {
      picCode: '',
      mobile: '',
      msgCode: '',
      picUrl: '',
      picKey: '',
      // 是否禁用按钮
      codeDisabled: false,
      // 倒计时秒数
      countdown: 5,
      // 按钮上的文字
      codeMsg: '获取验证码',
      // 定时器
      timer: null
    }
  },
  methods: {
    // 获取图形验证码
    async getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.picUrl = base64
      this.picKey = key
      this.$toast('提示文案')
    },
    // 校验手机号和图形验证码
    validFn () {
      // 手机号是否合法
      if (!/^1[3456789]\d{9}$/.test(this.mobile)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!this.picCode) {
        this.$toast('请输入图形验证码')
        return false
      }
      return true
    },
    // 发送短信验证码
    getValidStr () {
      console.log(this.countdown)
      if (this.countdown > 0 && this.countdown <= 5) {
        this.countdown--
        if (this.countdown !== 0) {
          this.codeMsg = '重新发送(' + this.countdown + ')'
        } else {
          clearInterval(this.timer)
          this.codeMsg = '获取验证码'
          this.countdown = 5
          this.timer = null
          this.codeDisabled = false
        }
      }
    },
    // 获取验证码
    async getCode () {
      if (!this.validFn()) return
      // 验证码60秒倒计时
      if (!this.timer) {
        // 发送验证码
        await getSmsCode(this.picCode, this.picKey, this.mobile)
        this.$toast('短信发送成功')

        this.getValidStr()
        this.timer = setInterval(() => {
          this.getValidStr()
          console.log('正在倒计时')
        }, 1000)
      }
    },

    // 登录
    async login () {
      if (!this.validFn()) return
      // 判断长度是否6位
      if (this.msgCode.length !== 6) {
        this.$toast('请输入6位短信验证码')
        return
      }

      const res = await codeLogin(this.mobile, this.msgCode)
      this.$store.commit('user/setUserInfo', res.data)
      this.$toast('登录成功')

      const url = this.$route.query.backUrl || '/'
      this.$router.replace(url)
    }
  },
  created () {
    this.getPicCode()
  },
  // 离开页面清除倒计时
  destroyed () {
    clearInterval(this.timer)
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;

    h3 {
      font-size: 26px;
      font-weight: normal;
    }

    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;

    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }

    img {
      width: 94px;
      height: 31px;
    }

    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, .1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
